package com.wxxymaker.model.dao;

import com.blade.ioc.annotation.Bean;
import com.wxxymaker.model.entity.ArtImage;
import io.github.biezhi.anima.Anima;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Soft
 * @Date: 2018/7/16 11:50
 * @Desc:
 */
@Bean
public class ArtImageDao {

    public void set(String key, List<String> urls) {
        if (key != null && urls != null) {
            if (isHasKey(key)) { // 修改前先清空
                this.clear(key);
            }
            for (String url : urls) {
                this.set(key, url);
            }
        }
    }

    public void set(String key, String path) {
        ArtImage artImage = new ArtImage();
        artImage.setFlagKey(key);
        artImage.setPath(path);

        artImage.save();
    }

    public List<String> get(String key) {
        List<String> urls = new ArrayList<>();
        Anima.select().from(ArtImage.class).where(ArtImage::getFlagKey, key).all().forEach(image -> {
            urls.add(image.getPath());
        });

        return urls;
    }

    public void clear(String key) {
        Anima.delete().from(ArtImage.class).where(ArtImage::getFlagKey, key).execute();
    }

    public boolean isHasKey(String key) {
        return Anima.select().from(ArtImage.class).where(ArtImage::getFlagKey, key).count() > 0;
    }
}
